package com.active.service.initacti;

import com.active.config.EnvConfig;
import com.alibaba.druid.pool.DruidDataSource;
import org.activiti.engine.*;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileUrlResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipInputStream;

/**
 * Created by Administrator on 2019/6/3 0003.
 */
@Configuration
public class InitActiviti {

    @Resource(name = "dataSource")
    private DruidDataSource druidDataSource;

    @Autowired
    private EnvConfig envConfig;


    //配置流程引擎，应用于springmvc
//    @Bean
//    public ProcessEngine init(){
//        return ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();
//    }
    //配置流程引擎，应用于springboot
    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(druidDataSource);
        return dataSourceTransactionManager;
    }


    @Bean
    public ProcessEngineConfiguration processEngineConfiguration(DataSourceTransactionManager dataSourceTransactionManager){
        SpringProcessEngineConfiguration springProcessEngineConfiguration = new SpringProcessEngineConfiguration();
        springProcessEngineConfiguration.setDataSource(druidDataSource);
        springProcessEngineConfiguration.setDatabaseSchemaUpdate("true");
        springProcessEngineConfiguration.setJobExecutorActivate(false);
        springProcessEngineConfiguration.setTransactionManager(dataSourceTransactionManager);
        //邮件系统初始化,死不好用，建议不用，利用他的反转，自己写都好一点
        springProcessEngineConfiguration.setMailServerHost(envConfig.getMailHost());
        springProcessEngineConfiguration.setMailServerPassword(envConfig.getMailPassword());
        springProcessEngineConfiguration.setMailServerUsername(envConfig.getMailUserName());
        springProcessEngineConfiguration.setMailServerPort(envConfig.getMailPort());

        //自动部署bpmn文件,测试直接配置死路径，若线上环境，可更改为服务器路径
//        String url = "E:/activiti/active/src/main/resources/diagrams";
//        File file = new File(url);
//        File[] files = file.listFiles();
//
//        if(null != files && files.length > 0){
//
//            for(int i=0; i < files.length; i++){
//                org.springframework.core.io.Resource[] resources = new org.springframework.core.io.Resource[0];
//                org.springframework.core.io.Resource resource = null;
//                try {
//                    resource = new FileUrlResource(url+"/"+files[i].getName());
//                } catch (MalformedURLException e) {
//                    e.printStackTrace();
//                }
//                //一个一个的添加，好维护后面如果修改了
//                resources[0] = resource;
//                springProcessEngineConfiguration.setDeploymentResources(resources);
//                springProcessEngineConfiguration.setDeploymentName("项目名称");
//            }
//
//        }


        String url = "E:/activiti/active/src/main/resources/zipResource/diagrams.zip";
        org.springframework.core.io.Resource resource = null;
        try {
            resource = new FileUrlResource(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        //当需要修改单个或者几个的时候，建议使用上面方法，这里为了方便，把所有的都打包为zip
        org.springframework.core.io.Resource[] resources = new org.springframework.core.io.Resource[1];
        resources[0] = resource;
        springProcessEngineConfiguration.setDeploymentResources(resources);
        springProcessEngineConfiguration.setDeploymentName("项目名称");//此处为项目名称
        return springProcessEngineConfiguration;
    }



    @Bean
    public ProcessEngine init(ProcessEngineConfiguration processEngineConfiguration){
        return processEngineConfiguration.buildProcessEngine();
    }

    @Bean
    public TaskService taskService(ProcessEngine processEngine){
        return processEngine.getTaskService();
    }
    @Bean
    public RuntimeService runtimeService(ProcessEngine processEngine){
        return processEngine.getRuntimeService();
    }
    @Bean
    public RepositoryService repositoryService(ProcessEngine processEngine){
        return processEngine.getRepositoryService();
    }
    @Bean
    public IdentityService identityService(ProcessEngine processEngine){
        return processEngine.getIdentityService();
    }

    @Bean
    public HistoryService historyService(ProcessEngine processEngine){
        return processEngine.getHistoryService();
    }

    @Bean
    public FormService formService(ProcessEngine processEngine){
        return processEngine.getFormService();
    }
}
